package org.bluedream.comm.utils.QRC;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.HashMap;

/**
 * @ClassName QRCodeGenerator
 * @Description TODO
 * @Author foxsand
 * @Data 2020/5/19 17:02
 * @Version
 */
public class QRCodeGenerator {
    private static final String QR_CODE_IMAGE_PATH = "d:\\MyQRCode.jpg";

    private static void generateQRCodeImage(String msg , int width , int height , String filePath) throws WriterException, IOException {
        // 1、设置二维码的一些参数
        HashMap hints = new HashMap();

        // 1.1设置字符集
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");

        // 1.2设置容错等级；因为有了容错，在一定范围内可以把二维码p成你喜欢的样式
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);

        // 1.3设置外边距;(即白色区域)
        hints.put(EncodeHintType.MARGIN, 1);

        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode(msg , BarcodeFormat.QR_CODE , width , height , hints);
        Path path = FileSystems.getDefault().getPath(filePath);
        MatrixToImageWriter.writeToPath(bitMatrix , "JPG" , path);

        addLogo(new File(QR_CODE_IMAGE_PATH) , new File("d:\\czclogo.jpg") , new LogoConfig());
    }

    public static void generateQRCodeImage(String msg) throws IOException, WriterException {
        generateQRCodeImage(msg , 350 , 350 , QR_CODE_IMAGE_PATH);
    }

    public static void main(String[] args) {
        try {
            generateQRCodeImage("https://www.wjx.cn/vj/eIehIid.aspx" , 350 , 350 , QR_CODE_IMAGE_PATH);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void addLogo(File qrFile , File logoFile , LogoConfig logoConfig){
        if (!qrFile.isFile() || !logoFile.isFile()){
            throw new RuntimeException("无法找到文件");
        }
        try {
            // 读取二维码图片，并构建绘图对象
            BufferedImage qrImage = ImageIO.read(qrFile);
            Graphics2D graphics2D = qrImage.createGraphics();

            // 读取logo图片
            BufferedImage logoImage = ImageIO.read(logoFile);

            int widthLogo = qrImage.getWidth() / logoConfig.getLogoPart();
            int heightLogo = qrImage.getHeight() / logoConfig.getLogoPart();

            // 计算图片放置位置
            int x = (qrImage.getWidth() - widthLogo) / 2;
            int y = (qrImage.getHeight() - heightLogo) / 2;

            // 绘制图片
            graphics2D.drawImage(logoImage , x , y , widthLogo , heightLogo , null);
            graphics2D.drawRoundRect(x , y , widthLogo , heightLogo , 10, 10);
            graphics2D.setStroke(new BasicStroke(logoConfig.getBorder()));
            graphics2D.setColor(logoConfig.getBorderColor());
            graphics2D.drawRect(x , y , widthLogo , heightLogo);

            graphics2D.dispose();

            ImageIO.write(qrImage , "jpeg" , new File(QR_CODE_IMAGE_PATH));
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
